package au.edu.unsw.editingtool.global;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;

public class EditingToolPropertyReader {

    public static final String LDAP = "ldap";
    public static final String FEDORA = "fedora";
    public static final String DATABASE = "database";
    public static final String DATASTREAM = "datastream";

    private Hashtable<String, String> attributes;
    private String filter;
    private String file;

    public EditingToolPropertyReader() {
        this.attributes = new Hashtable<String, String>();
        this.filter = "";
        this.file = "";
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }

    public void setFile(String path) {
        this.file = path;
    }
    
    public Hashtable<String, String> getProperties() {
        return this.attributes;
    }
    
    public void clean() {
    	this.attributes.clear();
    }

    public void retrieveProperties() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(this.file));
        String line;
        String currLine[] = null;
        
        while ((line = br.readLine()) != null) {
            if (line.trim().isEmpty() || (line.trim().charAt(0) == '#'))
                continue;
            currLine = line.split("=", 2);
            if (currLine.length != 2)
                continue;
            if (currLine[0].trim().regionMatches(0, this.filter, 0, this.filter.length()))
                this.attributes.put(currLine[0].trim().substring(this.filter.length()+1), currLine[1].trim());
        }
    }
}
